# -*- coding: utf-8 -*-
import sys
from solid import *
import os
import solid.utils as u
from euclid3 import Point2
import sys, math

sys.path.append("C://Users/DELL/Documents/OpenSCAD/libraries")


# https://github.com/JohK/nutsnbolts

class YunZ:
    def __init__(self):
        self.res_dir = "./result"
        self.nutsn = import_scad('./nutsnbolts-master/cyl_head_bolt.scad')
        self.res_dir = "./result"
        self.hou = 4
        self.motor_up = 20
    
    def look(self, mod, name='temp'):
        f = f'{self.res_dir}/{name}.scad'
        f = os.path.abspath(f)
        print("scad file==", f)
        scad_render_to_file(mod, f)
        os.system(f"openscad {f}")
    
    def render(self, mod, name):
        f = f'{self.res_dir}/{name}.scad'
        f = os.path.abspath(f)
        scad_render_to_file(mod, f)
        os.system(f"openscad -o {self.res_dir}/{name}.stl {f}")
    
    def zhuan_pan_1(self):
        lianjie = u.down(self.hou)(self._zhuan_pan_1_lianjie())
        bot=self._zhuan_pan_1_sui_pin()-lianjie
        self.look(bot)
        self.render(bot,"bottom")
        res = u.down(10)(bot)  + self._zhuan_pan_1_left() + self._zhuan_pan_1_right()
        return res
    
    def assembly(self):
        su_zhi = u.up(self.motor_up + 23.05 - 5.71)(self.zhuan_pan_2())

        sui_pin = self.zhuan_pan_1()
        # bot=self.bottom()
        # bot1=u.down(30)(bot)
        # bot2=rotate([0,0,180])(bot1)
        # res=su_zhi+sui_pin+bot1+u.left(40)(bot2)
    
    def _zhuan_pan_1_lianjie(self):
        luo_kong_inner = 1.7 / 2
        center_inner = 4.7 / 2
        r = 5.335
        hole = cylinder(r=luo_kong_inner, h=self.hou)
        center_hole = cylinder(r=center_inner, h=self.hou)
        hs = [center_hole]
        for i in range(8):
            ang = math.radians(i * 45)
            hs.append(translate([r * math.cos(ang), r * math.sin(ang), 0])(hole))
        holes = sum(hs)
        return holes
    
    def _zhuan_pan_1_sui_pin(self):
        # x=self.hou+11.12+self.hou+13.23
        x = 120
        c1 = cube([x, 40, self.hou], center=True)
        
        shift_x = x / 2 - 2*self.hou
        kong = cylinder(r=2, h=4, center=True)
        k1 = translate([-shift_x, 3.5, 0])(kong)
        k2 = translate([-shift_x, -3.5, 0])(kong)
        k3 = translate([shift_x, 3.5, 0])(kong)
        k4 = translate([shift_x, -3.5, 0])(kong)
        c1 = c1 - (k1 + k2 + k3 + k4)
        
        x = 48.4 / 2
        y = 26.8 / 2
        kong = cylinder(r=3 / 2, h=self.hou / 2)
        ks = translate([x, y, 0])(kong) + translate([-x, y, 0])(kong) + translate([x, -y, 0])(kong) + translate(
            [-x, -y, 0])(kong)
        ks2 = u.left(x - 9)(ks)
        
        c2 = cube([10, 5, self.hou], center=True)
        c3 = u.right(15)(c2)
        res=c1 - ks2 - c3
        # self.look(res)
        # self.render(res,"sui_pin")
        return res
    
    def _zhuan_pan_1_right(self):
        # motor up trans 20mm
        l = self.motor_up + (23 - 5.71) + 9 + self.hou
        w = 21
        an_zhuang_zu = cube([self.hou , w, self.hou * 2], center=True)
        kong = rotate([0, 90, 0])(cylinder(r=2, h=self.hou))
        k1 = translate([-self.hou/2, 3.5, 0])(kong)
        k2 = translate([-self.hou/2, -3.5, 0])(kong)
        an_zhuang_zu = an_zhuang_zu - (k1 + k2)
        an_zhuang_zu = translate([-(l / 2 - self.hou/2), 0, 1.5*self.hou])(an_zhuang_zu)
        
        c1 = cube([l, w, self.hou], center=True) + an_zhuang_zu
        cy1 = cylinder(r=9, h=self.hou, center=True)
        cy2 = translate([self.motor_up + 23 - 5.71 - l / 2, 0, 0])(cy1)
        c2 = c1 - cy2
        c3 = rotate([0, -90, 0])(c2)
        c4 = translate([60 - self.hou / 2, 0, l / 2])(c3)
        # self.render(rotate([0,90,0])(c4),"right")
        return c4
    
    def _zhuan_pan_1_left(self):
        l = 23 + self.motor_up + (9 + self.hou - 5.71)
        w = 21
        an_zhuang_zu = translate([0, 0, -2 * self.hou])(cube([self.hou , w, self.hou * 2]))
        kong = rotate([0, 90, 0])(cylinder(r=2, h=4))
        k1 = translate([0, 7, -self.hou])(kong)
        k2 = translate([0, 14, -self.hou])(kong)
        an_zhuang_zu = an_zhuang_zu - (k1 + k2)
        
        c1 = cube([l, w, self.hou]) + an_zhuang_zu
        f1 = cube([23+0.5, 12+0.5, self.hou])
        hs = self._fei_te_install_kong()
        
        # motor up trans 20mm
        f2 = translate([self.motor_up, (w - 12) / 2])(f1 + hs)
        
        c2 = rotate([0, -90, 0])(c1 - f2)
        c3 = translate([-(60 - self.hou), -(w - 9), 0])(c2)
        
        # self.look( rotate([0, -90, 0])(c3))
        # self.render(rotate([0, -90, 0])(c3),"left")
        
        return c3
    
    def _fei_te_install_kong(self):
        hole = cylinder(r=2.5 / 2, h=self.hou)
        hole2box_x = (32 - 23) / 2
        hole2box_y = (12 - 4 - 2.5 * 2) / 2
        h1 = translate([-hole2box_x, hole2box_y, 0])(hole)
        h2 = translate([-hole2box_x, 12 - hole2box_y, 0])(hole)
        h3 = translate([23 + hole2box_x, 12 - hole2box_y, 0])(hole)
        h4 = translate([23 + hole2box_x, hole2box_y, 0])(hole)
        hs = (h1 + h2 + h3 + h4)
        return hs
    
    # on the top
    def zhuan_pan_2(self):
        l = 120 - (31.9 - 20.5) - self.hou
        lianjie = self._zhuan_pan_2_lianjie()
        lr = rotate([0, 180, 0])(lianjie)
        bot = self._zhuan_pan_2_bottom()
        
        r_qie_diao = cube([self.hou, (40 - (18-1)) / 2, self.hou], center=True)
        shifty = (40 - (18-1)) / 2 / 2 + 9-0.5
        qie_diao = translate([(l - self.hou) / 2, shifty, 0])(r_qie_diao) + translate([(l - self.hou) / 2, -shifty, 0])(r_qie_diao)
        
        holes = self._zhuan_pan_1_lianjie()
        holes2 = rotate([0, 90, 0])(holes)
        holes3=u.left(l / 2  )(holes2)
        
        res1 = u.left(l / 2 - self.hou / 2)(lianjie) + u.right(l / 2 - self.hou / 2)(lr) + bot  -qie_diao-holes3
        res2 = u.right((120 - l) / 2)(res1)
        res3=rotate([0,-90,0])(res2)
        # self.look(res3)
        # self.render(res3,"zhuanpan2")
        
        return res2
    
    def _zhuan_pan_2_bottom(self):
        l = 120 - (31.9 - 20.5) - self.hou
        c1 = cube([l, 40, self.hou], center=True)
        box_w = 73.23
        box_front = 13.5
        box_kong = cylinder(r=4 / 2, h=self.hou ,center=True)
        box_ks = translate([-box_w / 2, -box_front,0])(box_kong) + translate([box_w / 2, -box_front,0])(box_kong)
        
        bubble_dis = 30.98
        bub_kong = cylinder(r=2.6 / 2, h=self.hou*2,center=True )
        bubs = bub_kong + u.right(bubble_dis)(bub_kong)
        bubs2 = translate([10, 15, 0])(bubs)
   
        res = c1 - box_ks - bubs2
        return res
    
    def _zhuan_pan_2_lianjie(self):
        cy1 = cylinder(r=9-0.5, h=self.hou, center=True)
        t = math.tan(math.radians(45))
        con = linear_extrude(height=9 / t, scale=0)(circle(r=9-0.5))
        cy2 = cy1 + u.up(self.hou / 2)(con)
        cy3 = rotate([0, 90, 0])(cy2)
        return cy3
    
    def bottom(self):
        c1 = self._bottom_convex()
        leg = self._bottom_leg()
        res = c1 + translate([(21 - 5) / 2 + 5, 0, self.hou / 2])(leg)
        res = rotate([0, 0, 180])(res)
        return res
    
    def _bottom_convex(self):
        hole = cylinder(r=2.5 / 2, h=self.hou / 2)
        hole2box_x = 8.21 - 5.71
        hole2box_y = (12.05 - 4.57 - 5) / 2
        h1 = translate([hole2box_x, hole2box_y, 0])(hole)
        h2 = translate([hole2box_x, -hole2box_y, 0])(hole)
        hs = (h1 + h2)
        c1 = cube([5, 14, self.hou], center=True)
        
        res = c1 - u.left(5 / 2)(hs)
        return res
    
    def _bottom_leg(self):
        leg = cube([21, 21, 20.5], center=True)
        cy2 = cylinder(r=5, h=15, center=True)
        trans = (20.5 - 15) / 2
        leg2 = leg - u.down(trans)(cy2)
        nut1 = self.nutsn.nut("M12", thread="modeled")
        
        res = leg2 + u.down(20.05 / 2 - 10.6)(nut1)
        return u.down(20.5 / 2)(res)
    
    def _bottom_bolts(self):
        b = self.nutsn.screw("M10x16", thread="modeled")
        return b
    
    def arc(self, r):
        d = 225
        d2 = math.radians(d)
        a = u.arc(rad=r, start_degrees=0, end_degrees=d)
        ps = [Point2(0, 0), Point2(r, 0), Point2(r * math.cos(d2), r * math.sin(d2))]
        po = polygon(ps)
        b = u.linear_extrude(18)(po + a)
        return b
    
    def zou_tao(self):
        a1 = self.arc(15)
        a2 = self.arc(8)
        self.gen_stl(a1 - a2, "frame")
    
    def bolt8_nut10(self):
        # data-metric_cyl_head_bolts.scad 查阅型号
        b1 = self.nutsn.screw("M8x25", thread="modeled")
        b2 = translate([20, 0, 0])(b1)
        b3 = rotate([0, 180, 0])(b2)
        nut1 = self.nutsn.nut("M10", thread="modeled")
        self.gen_stl(b3, "screw")
        self.gen_stl(nut1, "nut")
    def _draw_polygon(self,l=7.93+0.4):
        r = l / 2
        ps=[]
        pt=[]
        for i in range(7):
            ang=math.radians(i*60)
            ps.append(Point2(r * math.cos(ang), r * math.sin(ang)))
            pt.append([7,i,i+1])
        ps.append(Point2(0, 0))
        poly=polygon(points=ps, paths=pt)
        res=u.linear_extrude(height=35)(poly)
        return res


    def base(self):
        d1=7.23+0.2
        inner=self._draw_polygon(d1)
        d2=d1+self.hou*2
        outer=self._draw_polygon(d2)
        col=u.back(d1/2)(outer-inner)

        f_l=23 + 0.6
        f_w=12 + 0.6
        c1 = cube([f_l+self.hou*3, f_w+self.hou*3, 35])
        f1 = u.translate([self.hou*1.5,self.hou*1.5,5])(cube([f_l,f_w ,30]))

        col=u.right(f_l/2+d1/2)(col)
        res1=c1-f1+col
        self.look(res1)
        self.render(res1,"base")



if __name__ == '__main__':
    p = YunZ()
    p.base()
    # p.assembly()
    # p.render()
    # p.bottom()
